<template>
  <el-form size="large" class="login-content-form">
    <el-form-item class="login-animation1">
      <el-input type="text" :placeholder="$t('message.account.accountPlaceholder1')" v-model="ruleForm.userName"
                clearable autocomplete="off">
        <template #prefix>
          <el-icon class="el-input__icon">
            <ele-User/>
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item class="login-animation2">
      <el-input
          :type="isShowPassword ? 'text' : 'password'"
          :placeholder="$t('message.account.accountPlaceholder2')"
          v-model="ruleForm.password"
          autocomplete="off"
      >
        <template #prefix>
          <el-icon class="el-input__icon">
            <ele-Unlock/>
          </el-icon>
        </template>
        <template #suffix>
          <i
              class="iconfont el-input__icon login-content-password"
              :class="isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
              @click="isShowPassword = !isShowPassword"
          >
          </i>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item class="login-animation3">
      <el-col :span="15">
        <el-input
            type="text"
            maxlength="4"
            :placeholder="$t('message.account.accountPlaceholder3')"
            v-model="ruleForm.captchaCode"
            clearable
            autocomplete="off"
        >
          <template #prefix>
            <el-icon class="el-input__icon">
              <ele-Position/>
            </el-icon>
          </template>
        </el-input>
      </el-col>
      <el-col :span="1"></el-col>
      <el-col :span="8" class="mt14"  @click="captchaEvent">

        <el-image  style="width: 100%;height: 40px" :src="captcha">
          <template #error>
            <div class="image-slot">
            验证码加载失败
            </div>
          </template>
        </el-image>
      </el-col>
    </el-form-item>
    <el-form-item class="login-animation4">
      <el-button type="primary" class="login-content-submit" round @click="onSignIn" :loading="loading.signIn">
        <span>{{ $t('message.account.accountBtnText') }}</span>
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { Picture as IconPicture } from '@element-plus/icons-vue'
import {toRefs, reactive, defineComponent, computed} from 'vue';
import Cookies from 'js-cookie';
import {initBackEndControlRoutes} from '/@/router/backEnd';
import {Session} from '/@/utils/storage';
import {formatAxis} from '/@/utils/formatTime';
import {getCaptcha} from "/@/api/login/captcha";
import {weblogin} from "/@/api/login";
export default defineComponent({
  name: 'loginAccount',
  setup() {
    const captcha = ref('')

    const state = reactive({
      isShowPassword: false,
      ruleForm: {
        userName: '',
        password: '',
        captchaCode: '',
        captchaUuid: ''
      },
      loading: {
        signIn: false,
      },
    });
    // 时间获取
    const currentTime = computed(() => {
      return formatAxis(new Date());
    });
    // 获取验证码
    const captchaEvent = () => {
      getCaptcha().then(res => {
        let {data} = res
        captcha.value = data.imageData
        state.ruleForm.captchaUuid = data.uuid
      })
    }
    onBeforeMount(() => {
      captchaEvent()
    })

    const onSignIn = async () => {
      state.loading.signIn = true
      await weblogin(state.ruleForm).then(res => {
        Session.set('satoken', res.data);
        Cookies.set('userName', state.ruleForm.userName);
        initBackEndControlRoutes();

      }).catch(()=>{
        state.loading.signIn = false
      })
    };
    return {
      captcha,
      captchaEvent,
      onSignIn,
      ...toRefs(state),
    };
  },
});
</script>

<style scoped lang="scss">
.image-slot{color:red}
.login-content-form {
  margin-top: 20px;
  @for $i from 1 through 4 {
    .login-animation#{$i} {
      opacity: 0;
      animation-name: error-num;
      animation-duration: 0.5s;
      animation-fill-mode: forwards;
      animation-delay: calc($i/10) + s;
    }
  }

  .login-content-password {
    display: inline-block;
    width: 20px;
    cursor: pointer;

    &:hover {
      color: #909399;
    }
  }

  .login-content-code {
    width: 100%;
    padding: 0;
    font-weight: bold;
    letter-spacing: 5px;
  }

  .login-content-submit {
    width: 100%;
    letter-spacing: 2px;
    font-weight: 300;
    //margin-top: 15px;
  }
}
</style>
